//
//  RequestCallBackDispth.m
//  MCF2
//
//  Created by QianDeng on 15/6/5.
//  Copyright (c) 2015年 ac. All rights reserved.
//

#import "RequestCallBackDispatch.h"

@implementation RequestCallBackDispatch

+ (id)shareRequestDispatch
{
    static RequestCallBackDispatch *dispatch = nil;
    if (!dispatch) {
        dispatch = [[[self class] alloc] init];
    }
    
    return dispatch;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.dispatchDict = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

/*添加请求回调方法
 *@param  reqName   请求名
 *@param  object   回调对象
 *@param  methodName   方法名
 *@return
 */
- (void)addRequestCallBackWithRequestName:(NSString *)reqName
                                className:(id)object
                               methodName:(NSString *)methodName
{
    if (object && methodName && reqName) {
        if (![self.dispatchDict objectForKey:reqName]) {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:object forKey:@"class"];
            [dict setObject:methodName forKey:@"methodName"];
            [self.dispatchDict setObject:dict forKey:reqName];
        }
    }

}


/*根据请求名回调分发
 *@param  name   请求名
 *@param  retDict  返回结果
 *@return
 */
- (void)callbackWithRequestName:(NSString *)name returnObject:(NSDictionary *)retDict
{
    NSDictionary *dict = [self.dispatchDict objectForKey:name];
    if (dict) {
        id object = [dict objectForKey:@"class"];
        NSString *methodName = [dict objectForKey:@"methodName"];
        if (object && methodName) {
            if ([object respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
                [object performSelector:NSSelectorFromString(methodName) withObject:retDict];
#pragma clang diagnostic pop
            }
        }
        
//        if ([NSClassFromString(className) respondsToSelector:@selector(shareHander)])
//        {
//            id obj = [NSClassFromString(className) shareHander];
//            if ([obj respondsToSelector:NSSelectorFromString(methodName)]) {
//                [obj performSelector:NSSelectorFromString(methodName) withObject:retDict];
//            }
//        }
    }
}

@end
